﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using WindowsFormsApplication1.WebTest.New;

namespace WindowsFormsApplication1.WebTest.WebTestXmlNodes
{
    [XmlRoot("WebTest", Namespace = XmlNamespace)]
    public class WebTestNode : GenericWebTestNode, IXmlSerializable
    {
        GenericWebTestNode _items;

        public override void SetNodeProperties()
        {
            SelectedImageIndex = 0;
            ImageIndex = 0;

            Text = _nodeProperties["Name"].Value;
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Notification.BeginOperation("Processing WebTest Node", NodeOperationEventArgs.EventImportance.Detailed);

            //Read Web Test attributes
            ReadXmlAttributes(reader);

            //Set node properties such as Icon, Text, etc..
            SetNodeProperties();

            //Move to the first element
            reader.MoveToContent();
            bool isEmptyElement = reader.IsEmptyElement;

            reader.Read();
            reader.MoveToContent();


            bool exitLoop = false;
            while (!isEmptyElement && reader.NodeType != XmlNodeType.EndElement)
            {
                //Process sub tags
                switch (reader.Name)
                {
                    case "Items":
                        ItemsNode requests =
                            (ItemsNode) _itemsNodeSerializer.Deserialize(reader);

                        foreach (GenericWebTestNode requestNode in requests.Nodes)
                            Nodes.Add(requestNode);

                        _items = requests;

                        break;

                    case "ValidationRules":
                        ValidationRulesNode validationRules =
                            (ValidationRulesNode)_validationRulesNodeSerializer.Deserialize(reader);
                        Nodes.Add(validationRules);
                        break;

                    case "ContextParameters":
                        ContextParametersNode contextParameters =
                            (ContextParametersNode)_contextParametersNodeSerializer.Deserialize(reader);
                        Nodes.Add(contextParameters);
                        break;

                    case "WebTestPlugins":
                        WebTestPluginsNode webTestPlugins =
                            (WebTestPluginsNode)_webTestPluginsNodeSerializer.Deserialize(reader);
                        Nodes.Add(webTestPlugins);
                        break;

                    case "DataSources":
                        DataSourcesNode dataSources =
                            (DataSourcesNode)_dataSourcesNodeSerializer.Deserialize(reader);
                        Nodes.Add(dataSources);
                        break;

                    default:
                        Notification.DebugMessage("Unknown tag: " + reader.Name);
                        exitLoop = true;
                        break;

                }

                reader.MoveToContent();
                if (exitLoop) break;
            }


            //If there is closing tag read it
            if (!isEmptyElement)
                reader.ReadEndElement();

            Notification.EndOperation();
        }

        #endregion
         
    }
}
